package config

import (
	"flag"
	"k8s.io/client-go/kubernetes"
	appsV1 "k8s.io/client-go/kubernetes/typed/apps/v1"
	coreV1 "k8s.io/client-go/kubernetes/typed/core/v1"
	"k8s.io/client-go/tools/clientcmd"
	metrics "k8s.io/metrics/pkg/client/clientset/versioned"
	beta1 "k8s.io/metrics/pkg/client/clientset/versioned/typed/metrics/v1beta1"
	"os"
	"path/filepath"
)

var Set = new(kubernetes.Clientset)
var MetricsSet = new(metrics.Clientset)

func init() {
	var kubeconfig *string
	if dir, _ := os.Getwd(); dir != "" {
		kubeconfig = flag.String("kubeconfig", filepath.Join(dir, "k8s-config"), "(optional) absolute path to the kubeconfig file")
	} else {
		kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
	}
	flag.Parse()
	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		panic(err.Error())
	}

	Set, err = kubernetes.NewForConfig(config)
	MetricsSet, err = metrics.NewForConfig(config)
}

func CoreV1() coreV1.CoreV1Interface {
	return Set.CoreV1()
}

func AppsV1() appsV1.AppsV1Interface {
	return Set.AppsV1()
}

func NodeMetricses() beta1.NodeMetricsInterface {
	return MetricsSet.MetricsV1beta1().NodeMetricses()
}
